const LocalStorageManager = {
        set(key, value, expireDays) {
                try {
                        const data = {
                                value,
                                expire: expireDays ? Date.now() + expireDays * 24 * 60 * 60 * 1000 : null
                        };
                        localStorage.setItem(key, JSON.stringify(data));
                } catch (error) {
                        console.error('LocalStorage 存储失败:', error);
                }
        },
        get(key) {
                try {
                        const storedData = localStorage.getItem(key);
                        if (!storedData) return null;

                        const { value, expire } = JSON.parse(storedData);
                        if (expire && Date.now() > expire) {
                                this.remove(key);
                                return null;
                        }
                        return value;
                } catch (error) {
                        console.error('LocalStorage 读取失败:', error);
                        return null;
                }
        },
        remove(key) {
                localStorage.removeItem(key);
        },
        clear() {
                localStorage.clear();
        },
        increment(key, step = 1) {
                try {
                        let value = Number(this.get(key)) || 0;
                        value += step;
                        this.set(key, value);
                        return value;
                } catch (error) {
                        console.error('自增操作失败:', error);
                        return NaN;
                }
        },
        decrement(key, step = 1) {
                try {
                        let value = Number(this.get(key)) || 0;
                        value -= step;
                        this.set(key, value);
                        return value;
                } catch (error) {
                        console.error('自减操作失败:', error);
                        return NaN;
                }
        }
}
if (!LocalStorageManager.get("currentImageIndex")) {
        LocalStorageManager.set("currentImageIndex", 0)
}

class IndexMain {
        constructor(option) {
                this.baseURL = "/images_show/ArtificialIntelligenceTrainer_BasicKnowledge/";
                this.content = document.getElementById("content");
                this.catalogue = document.getElementById("catalogue");
                this.catalogueList = [];
                this.init();
        }
        init() {
                let someVariable = this;
                this.content.addEventListener('wheel', function(event) {
                        const currentScrollTop = someVariable.content.scrollTop;
                        const currentScrollHeight = someVariable.content.scrollHeight;
                        const clientHeight = someVariable.content.clientHeight;
                        if (currentScrollTop === 0 && event.deltaY < 0) {
                                console.log('滚动到顶部');
                                if (LocalStorageManager.get("currentImageIndex") > 0) {
                                        LocalStorageManager.decrement("currentImageIndex");
                                        someVariable.showImage(someVariable.catalogueList[LocalStorageManager.get("currentImageIndex")]);
                                        someVariable.content.scrollTo({
                                                top: someVariable.content.scrollHeight - someVariable.content.clientHeight,
                                                behavior: 'smooth'
                                        });
                                }
                        }
                        if (currentScrollTop + clientHeight >= currentScrollHeight && event.deltaY > 0) {
                                console.log('滚动到底部');
                                if (LocalStorageManager.get("currentImageIndex") < someVariable.catalogueList.length - 1) {
                                        LocalStorageManager.increment("currentImageIndex");
                                        someVariable.showImage(someVariable.catalogueList[LocalStorageManager.get("currentImageIndex")]);
                                }
                        }
                });
        }
        showImage(file_name) {
                this.content.innerHTML = "";
                let img = document.createElement("img");
                img.width = Math.floor(Number(this.content.scrollWidth) * 0.95);
                img.src = this.baseURL + file_name;
                this.content.append(img)
                $('#catalogue ul li').eq(LocalStorageManager.get("currentImageIndex"))
                        .addClass('active')
                        .siblings('li')
                        .removeClass('active');
        }
        json(url_dir) {
                this.baseURL = url_dir;
                let someVariable = this;
                $.getJSON(this.baseURL + "information.json", function(data) {
                        document.title = data.title;
                        someVariable.catalogueList = [...data.catalogue]
                        let ul = document.createElement("ul");
                        data.catalogue.map((item, index)=>{
                                let aa = document.createElement("a");
                                aa.id = index;
                                aa.href = "#";
                                aa.textContent = item;
                                aa.addEventListener('click', function(event) {
                                        LocalStorageManager.set("currentImageIndex", Number(event.target.id))
                                        someVariable.showImage(someVariable.catalogueList[LocalStorageManager.get("currentImageIndex")])
                                });
                                let li = document.createElement("li");
                                li.append(aa)
                                ul.append(li)
                        })
                        someVariable.catalogue.append(ul)
                        someVariable.showImage(someVariable.catalogueList[LocalStorageManager.get("currentImageIndex")]);
                })
        }
}

const IM = new IndexMain();

const testList = [
        ["人工智能训练师（基础知识）", "/images_show/ArtificialIntelligenceTrainer_BasicKnowledge/"]
]
const TI = document.getElementById("testid");
testList.map((item, index)=>{
        let aa = document.createElement("a");
        aa.href = "#";
        aa.textContent = item[0];
        aa.addEventListener('click', function() {
                IM.json(item[1]);
        });
        let li = document.createElement("li");
        li.append(aa);
        TI.append(li);
});